package admission.ecnu.service;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import admission.ecnu.dao.teacherInfoDao;
import admission.ecnu.interfaces.IRnsService;
import admission.ecnu.mainservlet.RnsContext;
import admission.ecnu.object.DataManager;
import admission.ecnu.object.OTeacherInfo;

public class updateTeacherInfo implements IRnsService {

	@Override
	public void executeService(RnsContext context) throws Exception {

		JSONObject obj = context.getJsonRequestObject();
		if(!DataManager.USER_ID.equals(obj.get("key"))){
			JSONObject jsonReturn = new JSONObject();
			jsonReturn.put("returnCode", DataManager.RETURN_WRONGKEY);
			context.setJsonResponseObject(jsonReturn);
			return;
		}
		
		JSONArray objArray = obj.getJSONArray("teacherInfo");
		
		ArrayList<OTeacherInfo> teacherList= new ArrayList<OTeacherInfo>();
		
		for(int i = 0; i< objArray.length(); i++){
			JSONObject teacherJson = objArray.getJSONObject(i);
			OTeacherInfo newTeacher  = new OTeacherInfo();
			
			newTeacher.setName(teacherJson.getString("name"));
			newTeacher.setTitle(teacherJson.getString("title"));
			if(teacherJson.getString("special")!=null&&teacherJson.getString("special").length()>0){
				newTeacher.setSpecial(teacherJson.getString("special"));
			}
			
			teacherList.add(newTeacher);
			System.out.print("new teacher"+i+": "+newTeacher.toString());
			
			teacherJson = null;
			newTeacher = null;
		}
		new teacherInfoDao().teacherUpdate(teacherList);
	}
}